package Offer来了.JUC容器.CopyOnWriteArrayList测试;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class CopyOnWriteArrayListTest {
    public static class ConcurrentTarget implements Runnable {
        //并发操作的目标队列
        List<String> targetList = null;

        public ConcurrentTarget(List<String> targetList) {
            this.targetList = targetList;
        }

        @Override
        public void run() {
            Iterator<String> iterator = targetList.iterator();
            //迭代操作
            while (iterator.hasNext()) {
                //在迭代操作时，进行列表的修改
                String threadName = Thread.currentThread().getName();
                System.out.println("开始往同步队列加入线程名称:" + threadName);
                targetList.add(threadName);
            }
        }

        //测试同步队列：在迭代操作时，进行列表的修改
        public static void main(String[] args) throws InterruptedException {
            List<String> notSafeList = Arrays.asList("a", "b", "c");
            List<String> synList = Collections.synchronizedList(notSafeList);
            //创建一个执行目标
            ConcurrentTarget concurrentTarget = new ConcurrentTarget(synList);
            //10个线程并发
            for (int i = 0; i < 10; i++) {
                new Thread(concurrentTarget, "线程" + i).start();
            }
            Thread.sleep(1000);
        }

    }
}
